﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp._console.core
{
    public partial class application_theme_assign : System.Web.UI.Page
    {
        private Guid _applicationId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                _applicationId = new Guid(Request.QueryString["id"]);
            }

            if (!IsPostBack)
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();

                if (_applicationId != Guid.Empty)
                {
                    SmartSpace.Core.Site application = service.GetSite(_applicationId);

                    _ctlLabelName.Text = application.Name;
                    _ctlLabelType.Text = application.Type;
                    _ctlLabelTheme.Text = "none";

                    // 获取可用的主题
                    List<SiteTemplate> themes = service.GetSiteTemplates(application.Type);
                    foreach (var item in themes)
                    {
                        if (application.TemplateId!= null && item.Id == application.TemplateId)
                        {
                            _ctlLabelTheme.Text = item.Name;
                            themes.Remove(item);
                            break;
                        }
                    }
                    _ctlGridTheme.DataSource = themes;
                    _ctlGridTheme.DataBind();
                }
            }
            _ctlLinkBack.NavigateUrl = "./application-list.aspx";
        }

        protected void _ctlBtnEnable_Click(object sender, EventArgs e)
        {
            LinkButton btn = sender as LinkButton;
            Guid themeId = new Guid(btn.CommandArgument.ToString());
            ICoreService service = ServiceLocator.Get<ICoreService>();
            SmartSpace.Core.Site app = service.GetSite(this._applicationId);
            if (app != null)
            {
                app.TemplateId = themeId;
                service.SaveSite(app);
            }
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }
}